package br.com.vdn.bean;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import br.com.util.exception.UtilException;
import br.com.util.jsf.AbstractBean;
import br.com.util.jsf.JSFUtil;
import br.com.util.tipodado.Flag;
import br.com.util.tipodado.YesNo;
import br.com.vdn.negocio.UsuarioNgc;
import br.com.vdn.vo.UsuarioVo;

public class UsuarioBean extends AbstractBean<UsuarioVo, UsuarioNgc>
{
	
	JSFUtil util = new JSFUtil();
	
	// senha default vdn
	private final String senhaDefault = "840BE3FCBD864653B4F0C0389B489F52";
	
	public UsuarioBean()
	{
		super(UsuarioNgc.getInstancia());
		this.ACTION_SEARCH = "usuario";
		this.pageTitle = "Usuario";
	}
	
	@Override
	protected void completarInserir() throws UtilException
	{
		this.getEntity().setFlagAtivo(new Flag(YesNo.YES));
		this.getEntity().setSenha(senhaDefault);
	}
	
	@Override
	protected void completarPesquisar() throws UtilException
	{
		this.getSearchObject().setFlagAtivo(new Flag(YesNo.YES));
	}
	
	public void resetarSenha(ActionEvent event)
	{
		try
		{
			this.getSelectedRowEntity().setSenha(senhaDefault);
			UsuarioNgc.getInstancia().alterar(this.getSelectedRowEntity());
			
			this.cancelar(event);
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, 
					"Senha resetada com sucesso!", null));
		}
		catch (UtilException ue)
		{
			FacesMessage message = new FacesMessage(ue.getMensagem());  
	        message.setSeverity(FacesMessage.SEVERITY_ERROR);  
	        FacesContext.getCurrentInstance().addMessage(null, message);
		}
	}
	
	public void inativar(ActionEvent event)
	{
		try
		{
			this.getSelectedRowEntity().setFlagAtivo(new Flag(YesNo.NO));
			UsuarioNgc.getInstancia().alterar(this.getSelectedRowEntity());
			
			this.cancelar(event);
			
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, 
					util.getBundle().getString("item.cancelado.sucesso"), null));
		}
		catch (UtilException ue)
		{
			FacesMessage message = new FacesMessage(ue.getMensagem());  
	        message.setSeverity(FacesMessage.SEVERITY_ERROR);  
	        FacesContext.getCurrentInstance().addMessage(null, message);
		}
	}

}
